﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Entities;
using System.Data;
using DigitalAnts.App_Code.BAL.Components;
using BAL.Components;

namespace DigitalAnts
{
    public partial class NewProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lbTitle.Text = "Đăng tin bán xe";
                this.btn_AddPro.Text = "Xác nhận đăng tin";
                doLoadDefaulData();

                fuplProductAvatar.AllowedFileTypes = "Tập tin hình ảnh:*.jpg;*.jpeg;*.gif;*.png";
                fuplProductImage.AllowedFileTypes = "Tập tin hình ảnh:*.jpg;*.jpeg;*.gif;*.png";
                fuplProductAvatar.MaxNumberFiles = 1;
                fuplProductAvatar.MaxFileNumberReachedMessage = "Bạn chỉ có thể chọn {0} tập tin";
                if (Request["id"] != null)
                {
                    this.lbTitle.Text = "Chỉnh sửa thông tin xe";
                    this.btn_AddPro.Text = "Xác nhận chỉnh sửa thông tin";
                    clsProduct obj = new clsProduct();
                    obj.ProductID = Convert.ToInt32(Request["id"]);
                    doLoadProducData(obj);
                }
            }
        }

        public void doLoadDefaulData()
        {
            doLoadCityList();
            doLoadFuelList();
            doLoadYearList();
            doLoadProducerList();
            doLoadCcList();
            doLoadYearList();
            doLoadLowerList();
        }

        public void doLoadProducData(clsProduct obj)
        {
            DataRow dr = clsProductBAL.doProductSelectID(obj).Rows[0];

            if (dr["colAntiThiefDevice"].ToString() == "1")
            {
                this.rbAntiTrue.Checked = true;
            }
            if (dr["IsNew"].ToString() == "1")
            {
                this.rdIsNew.Checked = true;
            }
            this.ddlCity.SelectedValue = dr["colCityID"].ToString();
            this.ddlFuel.SelectedValue = dr["colFuelID"].ToString();
            this.ddlProducer.SelectedValue = dr["colProducerID"].ToString();

            this.txtPrice.Text = dr["colPrice"].ToString();
            this.txtKm.Text = dr["colKilometersTraveled"].ToString();
            this.txtColor.Text = dr["colColor"].ToString();
        }
        
        protected void btn_AddPro_Click(object sender, EventArgs e)
        {
            int anti = 0;
            int isnew = 0;
            if (rbAntiTrue.Checked)
            {
                anti = 1;
            }
            if (rdIsNew.Checked)
            {
                isnew = 1;
            }
            clsProduct obj = new clsProduct();
            obj.Userid = Convert.ToInt32(Session["UserID"]);
            obj.CityID = Convert.ToInt32(ddlCity.SelectedValue);
            obj.FuelID = Convert.ToInt32(ddlFuel.SelectedValue);
            obj.Color = txtColor.Text.Trim();
            obj.KilometersTraveled = Convert.ToInt32(txtKm.Text.Trim());
            obj.Price = Convert.ToDouble(txtPrice.Text.Trim());
            obj.Content = this.txtContent.Text.Trim();
            obj.IsNew = isnew;
            obj.AntiThiefDevice = anti;
            obj.Cc = Convert.ToInt32(ddlCc.SelectedValue);
            obj.ProductionYear = Convert.ToInt32(ddlYear.SelectedValue);
            obj.ProducerID = Convert.ToInt32(ddlProducer.SelectedValue);
            obj.Title = txtProductTitle.Text.Trim();
            obj.ProductName = txtName.Text.Trim();
            if (this.btn_AddPro.Text == "Xác nhận chỉnh sửa thông tin")
            {
                clsProductBAL.doUpdateProduct(obj);
            }
            else
            {
                clsProductBAL.doAddNewProduct(obj);
            }
        }

        #region --- Default Data

        public void doLoadCityList()
        {
            this.ddlCountry.AppendDataBoundItems = true;
            this.ddlCountry.Items.Insert(0, new ListItem("Việt Nam", "1"));
            this.ddlCountry.Enabled = false;

            clsCountry obj = new clsCountry();
            obj.CountryID = "1";
            DataTable dt = clsCountryBAL.doGetCity(obj);
            this.ddlCity.DataSource = dt;
            this.ddlCity.DataTextField = "CityName";
            this.ddlCity.DataValueField = "CityID";
            this.ddlCity.DataBind();
        }

        public void doLoadCcList()
        {
            int[] cc = { 50, 100, 200 };
            this.ddlCc.DataSource = cc;
            this.ddlCc.DataBind();
        }

        public void doLoadProducerList()
        {
            DataTable dt = clsProducerBAL.doProducerSelectList();
            this.ddlProducer.DataSource = dt;
            this.ddlProducer.DataTextField = "colProducer";
            this.ddlProducer.DataValueField = "colProducerID";
            this.ddlProducer.DataBind();
            this.ddlProducer.AppendDataBoundItems = true;
            this.ddlProducer.Items.Insert(dt.Rows.Count, new ListItem("Khác ...", "0"));

        }

        public void doLoadFuelList()
        {
            DataTable dt = clsFuelBAL.doFuelSelectList();
            this.ddlFuel.DataSource = dt;
            this.ddlFuel.DataTextField = "Fuel";
            this.ddlFuel.DataValueField = "FuelID";
            this.ddlFuel.DataBind();
        }

        public void doLoadYearList()
        {
            int yNow = DateTime.Now.Year;
            string y = "";
            for (int i = yNow; i >= 1945; i--)
            {
                y += i + ",";
            }
            y = y.Substring(0, y.Length - 1);
            string[] yList = y.Split(',');
            this.ddlYear.DataSource = yList;
            this.ddlYear.DataBind();
        }

        public void doLoadLowerList()
        {
            string[] lower = { "1 Tuần", "1 Tháng", "3 Tháng" };
            this.ddlLower.DataSource = lower;
            this.ddlLower.DataBind();
        }

        #endregion






    }
}